<?php

namespace app\admin\model\system;

use app\common\model\TimeModel;
use think\facade\Db;

class SystemGenTableColumn extends TimeModel
{

    //数据库字符串类型
    public static $COLUMNTYPE_STR = ["char", "varchar", "narchar", "varchar2", "tinytext", "text", "mediumtext", "longtext"];

    //数据库时间类型
    public static $COLUMNTYPE_TIME = ["datetime", "time", "date", "timestamp"];

    //数据库数字类型
    public static $COLUMNTYPE_NUMBER = ["tinyint", "smallint", "mediumint", "int", "number", "integer", "bigint", "float", "float", "double", "decimal"];

    //页面不需要编辑字段
    public static $COLUMNNAME_NOT_EDIT = ["id", "create_by", "create_time", "del_flag", "delete_time", "update_by", "update_time"];

    //页面不需要显示的列表字段
    public static $COLUMNNAME_NOT_LIST = ["id", "create_by", "create_time", "del_flag", "delete_time", "update_by", "update_time"];

    //页面不需要查询字段
    public static $COLUMNNAME_NOT_QUERY = ["id", "create_by", "create_time", "del_flag", "delete_time", "update_by", "update_time", "remark"];

    public static function SelectDbTableColumnsByName($tableName)
    {
        $model = Db::table('information_schema.columns');

        $model->WhereRaw("table_schema = (select database())");
        $model->Where("table_name", $tableName)->order("ordinal_position");
        $model->fieldRaw("column_name, (case when (is_nullable = 'no' && column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type");

        return $model->select();
    }
    //查询业务字段列表
    public static function SelectGenTableColumnListByTableId($tableId) {
        return static::where("table_id", $tableId)->order("sort")->select();
    }
}